package cat.controller;

import cat.entity.Article;
import cat.entity.Category;
import cat.entity.Tag;
import cat.entity.User;
import cat.service.ArticleService;
import cat.service.CategoryService;
import cat.service.TagService;
import cn.hutool.http.HtmlUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 文章控制层
 * author:zhang
 */

@Controller
@RequestMapping("/article")
public class ArticleController {

    @Resource
    ArticleService articleService;

    @Resource
    CategoryService categoryService;

    @Resource
    TagService tagService;

    /**
     * 转到文章列表页
     * @param pageIndex 用于分页,表是当前是第几页
     * @param pageSize 用于分页,表示每页显示多少条数据
     */
    @RequestMapping(value = "")
    public String index(@RequestParam(required = false,defaultValue = "1") Integer pageIndex,
                        @RequestParam(required = false,defaultValue = "10") Integer pageSize, ModelMap m){

        PageInfo<Article> articlePageInfo = articleService.getPageArticleList(pageIndex,pageSize);

        m.put("pageInfo",articlePageInfo);

        m.put("pageUrlPrefix","article?pageIndex");  //把前缀传给分页的页面
        return "/Article/article-list";
    }



    @RequestMapping("/delete/{articleId}")
    public String deleteArticle(@PathVariable("articleId") Integer articleId){
        System.out.println(articleId);
        articleService.deleteArticleById(articleId);
        return "redirect:/article";
    }


    /**
     * 去文章添加页面
     * @param m
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String toArticleAddPage(ModelMap m){
        //分类信息
        List<Category> categoryList = categoryService.listCategory();
        //标签信息
        List<Tag> tagList = tagService.listTag();
        //带过去
        m.put("categoryList",categoryList);
        m.put("tagList",tagList);
        return "Article/article-add";
    }


    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String addArticle(HttpServletRequest request){
        Article article=new Article();

        //用户ID
        User user=(User)request.getSession().getAttribute("session_user");
        if(user!=null) {
            article.setArticleUserId(user.getUserId());
        }

        //文章标题
        article.setArticleTitle(request.getParameter("articleTitle"));

        //文章内容
        article.setArticleContent(request.getParameter("articleContent"));

        //文章状态
        article.setArticleStatus(Integer.parseInt(request.getParameter("articleStatus")));

        //文章摘要
        String str = HtmlUtil.cleanHtmlTag( article.getArticleContent());
        article.setArticleSummary(str.length()>150?str.substring(0,150):str);

        //一级分类ID
        String articleParentCategoryId=request.getParameter("articleParentCategoryId");

        //二级分类ID
        String articleChildCategoryId=request.getParameter("articleChildCategoryId");

        //添充article中的分类列表
        List<Category> categoryList=new ArrayList<Category>(2);
        categoryList.add(new Category(Integer.parseInt(articleParentCategoryId)));
        categoryList.add(new Category(Integer.parseInt(articleChildCategoryId)));
        article.setCategoryList(categoryList);

        //标签
        String [] tagIds =request.getParameterValues("articleTagIds");

        //填充article中的标签列表
        List<Tag> tagList=new ArrayList<Tag>();
        for(String tagId:tagIds) {
            tagList.add( new Tag(Integer.parseInt(tagId)));
        }

        article.setTagList(tagList);

        articleService.addArticle(article);

        //直接转到文章列表页
        return "forward:/article";

    }

    /**
     * 去文章分类界面
     * @return
     */
    @RequestMapping("/category")
    public String toArticleCategory(ModelMap m){
        List<Category> categoryList = categoryService.selectAllCategory();
        m.put("categoryList",categoryList);

        return "Article/article-category";
    }


    @RequestMapping("/deleteCategory")
    public String deleteCategoryById(Integer categoryId){
        System.out.println(categoryId);
        categoryService.deleteCategoryById(categoryId);
        return "redirect:/article/category";
    }


    @RequestMapping("/addCategory")
    public String addCategory(Category category){
        System.out.println(category);
        categoryService.addCategory(category);
        return "redirect:/article/category";
    }


    @RequestMapping("/toUpdateCategory")
    public String toUpdateCategory(ModelMap m,Integer categoryId){
        List<Category> category = categoryService.selectCategoryById(categoryId);
        List<Category> categoryList = categoryService.selectAllCategory();
        m.put("category",category);
        m.put("categoryList",categoryList);
        return "Article/article-categoryUpdate";
    }

    @RequestMapping("/updateCategory")
    public String updateCategory(Category category){
        System.out.println(category);
        categoryService.updateCategory(category);
        return "redirect:/article/category";
    }

    @RequestMapping("/tag")
    public String toArticleTag(ModelMap m){
        List<Tag> tagList = tagService.selectAllTag();

        m.put("tagList",tagList);
        return "Article/article-tag";
    }


    @RequestMapping("/deleteTag")
    public String deleteTagById(Integer tagId){
        tagService.deleteTagById(tagId);
        return "redirect:/article/tag";
    }

    @RequestMapping("/addTag")
    public String addTag(Tag tag){
        tagService.addTag(tag);

        return "redirect:/article/tag";
    }


    @RequestMapping("/toUpdateTag")
    public String toAddTag(Integer tagId, ModelMap m){
        List<Tag> tagList = tagService.selectTagById(tagId);
        m.put("tag",tagList);
        return "Article/article-tagUpdate";
    }

    @RequestMapping("/updateTag")
    public String updateTag(Tag tag){
        tagService.updateTag(tag);
        return "redirect:/article/tag";
    }


}
